package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.BaseRespData;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.admin.system.*;
import com.cskaoyan.service.admin.RoleService;
import com.cskaoyan.util.ValidationUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("admin/role")
public class RoleController {

    @Autowired
    RoleService roleService;

    @RequiresPermissions(value = {"*", "admin:role:read"}, logical = Logical.OR)
    @GetMapping("options")
    public BaseRespVo options() {

        List<RoleInfoOptionsVo> roleInfoOptionsVoList = roleService.getRoles();

        return BaseRespVo.ok(roleInfoOptionsVoList);
    }

    @RequiresPermissions(value = {"*", "admin:role:list"}, logical = Logical.OR)
    @GetMapping("list")
    public BaseRespVo list(String name, BaseParam param) {

        BaseRespData data = roleService.queryRoleList(name, param);
        return BaseRespVo.ok(data);
    }

    @RequiresPermissions(value = {"*", "admin:role:create"}, logical = Logical.OR)
    @PostMapping("create")
    public BaseRespVo create(@RequestBody @Validated RoleCreateBo roleAddBo, BindingResult bindingResult) {

        //参数校验
        BaseRespVo baseRespVo = ValidationUtils.dealWithFieldError(bindingResult);

        if(baseRespVo.getErrno() == 500){
            return baseRespVo;
        }


        RoleCreateVo roleCreateVo = roleService.createRole(roleAddBo);

        if (roleCreateVo == null) {
            return BaseRespVo.fail("角色已经存在");
        }

        return BaseRespVo.ok(roleCreateVo);
    }

    @RequiresPermissions(value = {"*", "admin:role:update"}, logical = Logical.OR)
    @PostMapping("update")
    public BaseRespVo update(@RequestBody @Validated RoleUpdateBo roleUpdateBo, BindingResult bindingResult) {

        //参数校验
        BaseRespVo baseRespVo = ValidationUtils.dealWithFieldError(bindingResult);

        if(baseRespVo.getErrno() == 500){
            return baseRespVo;
        }

        int updateCode = roleService.updateRole(roleUpdateBo);

        if (updateCode == 500) {
            return BaseRespVo.fail("修改后的角色与已有角色重复!");
        } else if (updateCode == 404) {
            return BaseRespVo.fail("系统错误,修改失败!");
        } else {
            return BaseRespVo.ok();
        }
    }

    @RequiresPermissions(value = {"*", "admin:role:delete"}, logical = Logical.OR)
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody RoleDeleteBo roleDeleteBo) {

        if(roleDeleteBo.getId() == 1 || roleDeleteBo.getId() == 2 || roleDeleteBo.getId() == 3){
            return BaseRespVo.fail("该角色无法删除!");
        }

        int deleteCode = roleService.delete(roleDeleteBo);
        return BaseRespVo.ok();
    }

    @RequiresPermissions(value = {"*", "admin:role:permission:get"}, logical = Logical.OR)
    @GetMapping("permissions")
    public BaseRespVo permissions(Integer roleId){

        PermissonsVo permissonsVo = roleService.getPermissions(roleId);

        return BaseRespVo.ok(permissonsVo);
    }

    @RequiresPermissions(value = {"*", "admin:role:permission:update"}, logical = Logical.OR)
    @PostMapping("permissions")
    public BaseRespVo permissions(@RequestBody PermissionsBo permissionsBo){

        if(permissionsBo.getRoleId() == 1){
            return BaseRespVo.fail("超级管理员的权限不允许修改!");
        }

        int permissionsCode = roleService.addPermissions(permissionsBo);
        return BaseRespVo.ok();
    }
}
